package com.hh.vietnameseproject.ui.register

import android.content.Intent
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.transition.Slide
import android.view.Gravity
import android.view.Window
import android.widget.Toast
import com.hh.vietnameseproject.databinding.ActivityRegisterBinding
import com.hh.vietnameseproject.logic.network.AllNetWork
import com.hh.vietnameseproject.ui.MainActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject

class RegisterActivity : AppCompatActivity() {

    lateinit var binding: ActivityRegisterBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityRegisterBinding.inflate(layoutInflater)
        window.requestFeature(Window.FEATURE_CONTENT_TRANSITIONS)
        val slide = Slide()
        slide.slideEdge = Gravity.RIGHT
        window.enterTransition = slide
        window.exitTransition = slide
        setContentView(binding.root)


        updateCode()


        binding.registerPhonecodeBtn.setOnClickListener {
            val phoneNum = binding.registerPhone.text.toString().trim()
            val phoneName = binding.registerName.text.toString().trim()
            val staticCode = binding.registerCode.text.toString().trim()

            if (phoneNum == null || phoneNum.equals("")) {
                Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (phoneName == null || phoneName.equals("")) {
                Toast.makeText(this, "请输入昵称", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (staticCode == null || staticCode.equals("")) {
                Toast.makeText(this, "请输入静态验证码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            binding.registerPhonecodeBtn.isEnabled = false
            GlobalScope.launch {
                val body = AllNetWork.getRegistPhoneCode(phoneNum, staticCode)
                val jsonObject = JSONObject(body.string())
                withContext(Dispatchers.Main) {
                    binding.registerReturnText.text = jsonObject.getString("message")
                }
                if (jsonObject.getInt("code") == 1) {
                    withContext(Dispatchers.Main) {
                        binding.registerPhonecodeBtn.isEnabled = false
                        binding.registerPhonecodeBtn.text = "0s后重发"
                    }
                    var i = 61
                    repeat(60) {
                        i--
                        withContext(Dispatchers.Main) {
                            binding.registerPhonecodeBtn.text = "${i}s后重发"
                        }
                        Thread.sleep(1000)
                    }
                    withContext(Dispatchers.Main) {
                        binding.registerPhonecodeBtn.isEnabled = true
                        binding.registerPhonecodeBtn.text = "获取验证码"
                    }

                } else {
                    updateCode()
                    withContext(Dispatchers.Main) {
                        binding.registerPhonecodeBtn.isEnabled = true
                    }
                }
            }


        }
        binding.registerBtn.setOnClickListener {
            val phoneNum = binding.registerPhone.text.toString().trim()
            val phoneName = binding.registerName.text.toString().trim()
            val phoneCode = binding.registerPhoneCode.text.toString().trim()

            if (phoneNum == null || phoneNum.equals("")) {
                Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (phoneName == null || phoneName.equals("")) {
                Toast.makeText(this, "请输入昵称", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (phoneCode == null || phoneCode.equals("")) {
                Toast.makeText(this, "请输入手机验证码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            GlobalScope.launch {
                val body = AllNetWork.registPhone(phoneNum, phoneCode, phoneName)
                val jsonObject = JSONObject(body.string())
                val code = jsonObject.getInt("code")
                if (code == 1) {
                    val body = AllNetWork.updateSchedule("1","5","0","20")
                    withContext(Dispatchers.Main) {
                        Toast.makeText(
                            this@RegisterActivity,
                            "${jsonObject.getString("message")}",
                            Toast.LENGTH_SHORT
                        ).show()
                        finish()
                    }

                } else {
                    withContext(Dispatchers.Main) {
                        binding.registerReturnText.text = jsonObject.getString("message")
                    }

                }

            }


        }
        binding.registerImagecode.setOnClickListener {
            updateCode()
        }
        binding.registerBack.setOnClickListener {
            onBackPressed()
        }

    }


    fun updateCode() {
        GlobalScope.launch {
            val body = AllNetWork.getCheckCode()
            withContext(Dispatchers.Main) {
                binding.registerImagecode.setImageBitmap(BitmapFactory.decodeStream(body.byteStream()))
            }
        }
    }

    override fun onBackPressed() {
        finishAfterTransition()
        super.onBackPressed()
    }


}